'use strict';

const builder = require('../filterBuilder');
const api = require('../dicAPIUrl');

function request(requestData) {
	// 解析过滤条件
	let filters = filter(requestData.condition);
	// 作业数
	let questionData = {
		// 主体目标
		'data': {
			'target': 'homeworkquestion-question_id',
			'data_type': 'count',
			'type': 'normal'
		},
		// 时间粒度
		'time_unit': '',
		// 分组显示标识
		'multi_unit': 'question-question_type',
		// 筛选条件
		'filter': filters
	};

	return [questionData];
}

function response(questionData) {
	let rn = {
			series: [{
				name: '题目数目',
				data: []
			}],
			categories: ['主观题', '客观题']
		},
		subjective = '23457',
		objective = '016';

	// debugger;
	let subjectiveData = 0,
		objectiveData = 0;

	questionData.forEach((item) => {
		if (objective.indexOf(item.multi_data) != -1) {
			objectiveData += parseInt(item.result);
		} else if (subjective.indexOf(item.multi_data) != -1) {
			subjectiveData += parseInt(item.result);
		}
	});

	rn.series[0].data.splice(0, 0, subjectiveData, objectiveData);

	return rn;
}

function filter(requestData) {
	let filters = [];
	let condition = requestData;

	if (condition.province && condition.province != builder.NIL) {
		filters.push(builder.append('teacher-province_id', '=', condition.province));
	}
	if (condition.city && condition.city != builder.NIL) {
		filters.push(builder.append('teacher-city_id', '=', condition.city));
	}
	if (condition.county && condition.county != builder.NIL) {
		filters.push(builder.append('teacher-county_id', '=', condition.county));
	}
	if (condition.school && condition.school != builder.NIL) {
		filters.push(builder.append('teacher-school_id', '=', condition.school));
	}
	if (condition.stage[0] != builder.NIL) {
		filters.push(builder.append('teacher-grade_part', 'in', condition.stage.join(',')));
	}
	if (condition.grade[0] != builder.NIL) {
		filters.push(builder.append('teacher-grade', 'in', condition.grade.join(',')));
	}
	if (condition.subject[0] != builder.NIL) {
		filters.push(builder.append('teacher-subject', 'in', condition.subject.join(',')));
	}
	if (condition.version != '0') {
		filters.push(builder.append('teacher-version', '=', condition.version));
	}
	if (condition.deviceType[0] != builder.NIL) {
		filters.push(builder.append('teacher-source', 'in', condition.deviceType.join(',')));
	}

	filters.push(builder.append('homework-day', '>=', condition.startDate));
	filters.push(builder.append('homework-day', '<=', condition.endDate));
	filters.push(builder.append('teacher-student_count', '>=', condition.userFeature));
	// filters.push(builder.append('teacher-student_count', '<=', condition.userFeature.end));

	return filters;
}

module.exports = {
	API_URL: api.API_PACKAGE_DATA,
	request,
	response
}